package com.lyh.hkcloud.util.base;

import android.os.Handler;
import android.text.InputType;
import android.view.*;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import com.lyh.hkcloud.base.ModuleActivity;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class ViewUtil {


    public static void clearView(ViewGroup vg) {
        int count = vg.getChildCount();
        for (int i = 0; i < count; i++) {
            View view = vg.getChildAt(i);
            if (view instanceof EditText) {
                ((EditText) view).setText("");
            } else if (view instanceof CheckBox) {
                ((CheckBox) view).setChecked(false);
            } else if (view instanceof ViewGroup) {
                clearView((ViewGroup) view);
            }
        }
    }

    public static void setShowSoftInputOnFocus(ViewGroup vg) {
        setShowSoftInputOnFocus(vg, null);
    }

    public static void setShowSoftInputOnFocus(ViewGroup vg, ModuleActivity.OnEnterEventListener enterEventListener, View... views) {
        setShowSoftInputOnFocus(vg, enterEventListener, true, views);
    }

    public static void setShowSoftInputOnFocus(ViewGroup vg, ModuleActivity.OnEnterEventListener enterEventListener) {
        setShowSoftInputOnFocus(vg, enterEventListener, true);
    }

    private static Set<Integer> getIds(View... views) {
        Set<Integer> ids = new HashSet<>();
        if (views == null) {
            return ids;
        }
        for (View view : views) {
            ids.add(view.getId());
        }
        return ids;
    }

    public static void setShowSoftInputOnFocus(ViewGroup vg, ModuleActivity.OnEnterEventListener enterEventListener, boolean enabledInput, View... views) {
        Set<Integer> ids = getIds(views);
        int count = vg.getChildCount();
        for (int i = 0; i < count; i++) {
            View view = vg.getChildAt(i);
            if (view instanceof EditText) {
                EditText editText = (EditText) view;

                disableInput(editText, enterEventListener, enabledInput, view);
                if (!enabledInput && !ids.contains(view.getId())) {
                    disableCopyAndPaste(editText);
                    editText.setInputType(InputType.TYPE_NULL);
                }
            } else if (view instanceof ViewGroup) {
                setShowSoftInputOnFocus((ViewGroup) view, enterEventListener, enabledInput, views);
            }
        }
    }

    static long nowMill = 0;

    public static void disableInput(final EditText editText, final ModuleActivity.OnEnterEventListener enterEventListener, final boolean enabledInput, View... views) {
        final Set<Integer> ids = getIds(views);
        editText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                long thisMill = new Date().getTime();
                if (enterEventListener != null && keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    if (thisMill - nowMill < 500) {
                        return true;
                    }
                    nowMill = thisMill;
                    return enterEventListener.onEnter(editText);
                }
                if (!enabledInput && !ids.contains(v.getId())) {

                    if (((EditText) v).getText().toString().length() == 0) {
                        nowMill = thisMill;
                        if (keyCode != KeyEvent.KEYCODE_ENTER) {
                            clearOne((EditText) v);
                        }
                        return false;
                    }
                    if (thisMill - nowMill > 80 && keyCode != KeyEvent.KEYCODE_ENTER) {
                        ((EditText) v).setText("");
                        return false;
                    }
                    nowMill = thisMill;
                }
                return false;
            }
        });
    }

    static Handler handler = new Handler();

    private static void clearOne(final EditText editText) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (editText.getText().toString().length() == 1) {
                    editText.setText("");
                }
            }
        }, 80);
    }

    public static void disableCopyAndPaste(final EditText editText) {
        try {
            if (editText == null) {
                return;
            }

            editText.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return true;
                }
            });
            editText.setLongClickable(false);
            editText.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        // setInsertionDisabled when user touches the view
                        setInsertionDisabled(editText);
                    }

                    return false;
                }
            });
            editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    return false;
                }

                @Override
                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    return false;
                }

                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    return false;
                }

                @Override
                public void onDestroyActionMode(ActionMode mode) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void setInsertionDisabled(EditText editText) {
        try {
            Field editorField = TextView.class.getDeclaredField("mEditor");
            editorField.setAccessible(true);
            Object editorObject = editorField.get(editText);

            // if this view supports insertion handles
            Class editorClass = Class.forName("android.widget.Editor");
            Field mInsertionControllerEnabledField = editorClass.getDeclaredField("mInsertionControllerEnabled");
            mInsertionControllerEnabledField.setAccessible(true);
            mInsertionControllerEnabledField.set(editorObject, false);

            // if this view supports selection handles
            Field mSelectionControllerEnabledField = editorClass.getDeclaredField("mSelectionControllerEnabled");
            mSelectionControllerEnabledField.setAccessible(true);
            mSelectionControllerEnabledField.set(editorObject, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
